package com.etang.a2020d1021.tools;

import android.app.Activity;
import android.text.TextUtils;

import com.bizideal.smarthome.socket.ConstantUtil;
import com.bizideal.smarthome.socket.ControlUtils;
import com.bizideal.smarthome.socket.DataCallback;
import com.bizideal.smarthome.socket.DeviceBean;
import com.bizideal.smarthome.socket.LoginCallback;
import com.bizideal.smarthome.socket.SocketClient;

public class AppConfig {

    public static float temp = 0;
    public static float hum = 0;
    public static float smo = 0;
    public static float gas = 0;
    public static float press = 0;
    public static float ill = 0;
    public static float pm = 0;
    public static float co = 0;
    public static float per = 0;

    private static void server_getdata(final Activity activity) {
        ControlUtils.getData();
        SocketClient.getInstance().getData(new DataCallback<DeviceBean>() {
            @Override
            public void onResult(final DeviceBean getdata) {
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (!TextUtils.isEmpty(getdata.getAirPressure())) {
                            press = Float.valueOf(getdata.getAirPressure());
                        }
                        if (!TextUtils.isEmpty(getdata.getCo2())) {
                            co = Float.valueOf(getdata.getCo2());
                        }
                        if (!TextUtils.isEmpty(getdata.getGas())) {
                            gas = Float.valueOf(getdata.getGas());
                        }
                        if (!TextUtils.isEmpty(getdata.getHumidity())) {
                            hum = Float.valueOf(getdata.getHumidity());
                        }
                        if (!TextUtils.isEmpty(getdata.getSmoke())) {
                            smo = Float.valueOf(getdata.getSmoke());
                        }
                        if (!TextUtils.isEmpty(getdata.getIllumination())) {
                            ill = Float.valueOf(getdata.getIllumination());
                        }
                        if (!TextUtils.isEmpty(getdata.getPM25())) {
                            pm = Float.valueOf(getdata.getPM25());
                        }
                        if (!TextUtils.isEmpty(getdata.getTemperature())) {
                            temp = Float.valueOf(getdata.getTemperature());
                        }
                        if (!TextUtils.isEmpty(getdata.getStateHumanInfrared())) {
                            per = Float.valueOf(getdata.getStateHumanInfrared());
                        }
                    }
                });
            }
        });
    }

    public static void server_link(final Activity activity) {
        ControlUtils.setUser("bizideal", "123456", SocketClient.ip);
        SocketClient.getInstance().creatConnect();
        SocketClient.getInstance().login(new LoginCallback() {
            @Override
            public void onEvent(final String s) {
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (s.equalsIgnoreCase(ConstantUtil.SUCCESS)) {
                            DiyToast.showToast(activity, "网络连接成功");
                            server_getdata(activity);
                        } else {
                            DiyToast.showToast(activity, "网络连接失败");
                            server_getdata(activity);
                        }
                    }
                });
            }
        });
    }

}
